package dao;

import entity.Pet;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PetDao extends BaseDao {
    //连接数据库
    Connection conn = null;
    //执行SQL语句
    PreparedStatement pstmt = null;
    //保存数据
    ResultSet rs = null;

    /**
     * 查询全部信息
     */
    public List<Pet> allInfo(String sql) {
        List<Pet> list = new ArrayList<>();
        try {
            conn = super.getConnection();
            String SQL=sql;
            pstmt = conn.prepareStatement(SQL);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Pet pet = new Pet(rs.getInt("petId")
                        , rs.getString("petName")
                        , rs.getInt("petBreed")
                        , rs.getInt("petSex")
                        , rs.getString("birthday")
                        , rs.getString("description"));
                list.add(pet);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return list;
    }


    /**
     * 根据品种查询信息
     */
    public List<Pet> selectBreed(int breed) {
        List<Pet> list = new ArrayList<>();
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("select * from pet where petBreed=?");
            pstmt.setInt(1,breed);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Pet pet = new Pet(rs.getInt("petId")
                        , rs.getString("petName")
                        , rs.getInt("petBreed")
                        , rs.getInt("petSex")
                        , rs.getString("birthday")
                        , rs.getString("description"));
                list.add(pet);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return list;
    }

    /**
     * 根据昵称查找信息
     */
    public int selectName(String name) {
        int result=0;
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("select count(1) from pet where petName=?");
            pstmt.setString(1,name);
            rs = pstmt.executeQuery();
            if(rs.next()) {
               result=rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return result;
    }


    /**
     *增加
     */
    public int add(Pet pet){
        int result=0;
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("INSERT INTO `pet`( `petName`, `petBreed`, `petSex`, `birthday`, `description`) VALUES (?,?,?,?,?)");
            pstmt.setString(1,pet.getPetName());
            pstmt.setInt(2,pet.getPetBreed());
            pstmt.setInt(3,pet.getPetSex());
            pstmt.setString(4,pet.getBirthday());
            pstmt.setString(5,pet.getDescription());
            result=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return result;
    }
}
